/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "math.h"
#include "string.h"
#include "stdarg.h"
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
typedef enum
{
    MOTOR_FWD = 0,
    MOTOR_REV,
}motor_dir_t;

typedef struct
{
    motor_dir_t direction;    // 电机方向
    uint16_t dutyfactor;      // PWM 输出占空�???
    uint8_t is_enable;        // 使能电机
    uint32_t lock_timeout;    // 电机堵转计时
}bldcm_data_t;
#define PWM_MAX_PERIOD_COUNT    (5600 - 100)
#define ADC_NUM_MAX 320
#define VREF                            3.3f     // 参�?�电压，理论上是3.3
#define GET_ADC_VDC_VAL(val)            ((float)val/4096.0f*VREF)          // 得到电压�?
#define GET_ADC_CURR_VAL(val)           (((float)val)/(float)8.0/(float)0.02*(float)1000.0)        // 得到电流值，电压放大8倍，0.02是采样电阻，单位mA�?
#define GET_VBUS_VAL(val)               (((float)val - 1.24f) * 37.0f )      // 获取电压值（测量电压是电源电压的1/37�?

#define SPEED_FILTER_NUM 30
typedef struct{
    int32_t timeout;
    float speed;
    int32_t enable_flag;
    int32_t speed_group[SPEED_FILTER_NUM];
}motor_rotate_t;
/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define KEY2_Pin GPIO_PIN_13
#define KEY2_GPIO_Port GPIOC
#define KEY0_Pin GPIO_PIN_0
#define KEY0_GPIO_Port GPIOA
#define MOTOR_OCNPWM1_Pin GPIO_PIN_13
#define MOTOR_OCNPWM1_GPIO_Port GPIOB
#define MOTOR_OCNPWM2_Pin GPIO_PIN_14
#define MOTOR_OCNPWM2_GPIO_Port GPIOB
#define MOTOR_OCNPWM3_Pin GPIO_PIN_15
#define MOTOR_OCNPWM3_GPIO_Port GPIOB
#define KEY1_Pin GPIO_PIN_2
#define KEY1_GPIO_Port GPIOG
#define KEY3_Pin GPIO_PIN_3
#define KEY3_GPIO_Port GPIOG
#define KEY4_Pin GPIO_PIN_4
#define KEY4_GPIO_Port GPIOG
#define MOTOR_EA_HU_Pin GPIO_PIN_6
#define MOTOR_EA_HU_GPIO_Port GPIOC
#define MOTOR_EB_HV_Pin GPIO_PIN_7
#define MOTOR_EB_HV_GPIO_Port GPIOC
#define MOTOR_EZ_HW_Pin GPIO_PIN_8
#define MOTOR_EZ_HW_GPIO_Port GPIOC
#define MOTOR_SD_Pin GPIO_PIN_12
#define MOTOR_SD_GPIO_Port GPIOG

/* USER CODE BEGIN Private defines */
#define SET_BASIC_TIM_PERIOD(T)  __HAL_TIM_GET_AUTORELOAD(&htim7,(T)*50-1)
#define GET_BASIC_TIM_PERIOD()  ((__HAL_TIM_GET_AUTORELOAD(&htim7)+1)/50.0f)
#define BLDCM_ENABLE_SD() HAL_GPIO_WritePin(MOTOR_SD_GPIO_Port,MOTOR_SD_Pin,GPIO_PIN_SET)
#define BLDCM_DISABLE_SD() HAL_GPIO_WritePin(MOTOR_SD_GPIO_Port,MOTOR_SD_Pin,GPIO_PIN_RESET)
#define ReadKey0()  HAL_GPIO_ReadPin(KEY0_GPIO_Port,KEY0_Pin)
#define ReadKey1()  HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)
#define ReadKey2()  HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)
#define ReadKey3()  HAL_GPIO_ReadPin(KEY3_GPIO_Port,KEY3_Pin)
#define ReadKey4()  HAL_GPIO_ReadPin(KEY4_GPIO_Port,KEY4_Pin)
extern void set_pwm_pulse(uint16_t pulse);
extern void hall_enable(void);
extern void hall_disable(void);
extern uint8_t get_hall_state(void);
extern void set_bldcm_speed(uint16_t v);
extern void set_bldcm_direction(motor_dir_t dir);
extern motor_dir_t  get_bldcm_direction(void);
extern void set_bldcm_enable(void);
extern void set_bldcm_disable(void);
extern void stop_pwm_output(void);
extern int update_motor_tim;
extern motor_rotate_t motor_drive;
extern float  get_ntc_v_val(void);
extern float get_ntc_r_val(void);
extern float get_ntc_t_val(void);
extern int32_t get_curr_val_v(void);
extern int32_t get_curr_val_u(void);
extern int32_t get_curr_val_w(void);
extern float get_vbus_val(void);
extern float get_motor_speed(void);
/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
